package com.maybach.workflow.core.config.parser;


import com.maybach.workflow.common.api.enums.NodeTypeEnum;
import com.maybach.workflow.common.api.enums.TaskEnum;
import com.maybach.workflow.common.api.node.TaskActor;
import com.maybach.workflow.common.api.node.TaskNode;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by well on 15/6/11.
 */
public class TaskParser extends AbstractParser<TaskNode>{

    public TaskParser() {
        super(NodeParser.NODE_NAME_TASK);
    }


    @Override
    public TaskNode createNode() {
        return new TaskNode();
    }

    @Override
    public  void parseSpecial(Element element, TaskNode taskNode){

        taskNode.setUrl(element.getAttribute(NodeParser.ATTR_URL));
        taskNode.setBeforeHandlers(element.getAttribute(NodeParser.ATTR_BEFORE_HANDLERS));
        taskNode.setAfterHandlers(element.getAttribute(NodeParser.ATTR_AFTER_HANDLERS));
        taskNode.setParent(element.getAttribute(NodeParser.ATTR_PARENT));
        taskNode.setExecuteBeanName(element.getAttribute(NodeParser.ATTR_EXECUTE_BEAN_NAME));
        String taskType = element.getAttribute(NodeParser.ATTR_TASK_TYPE);
        if(StringUtils.hasText(taskType)){
            taskNode.setTaskType(TaskEnum.valueOf(taskType));

        }else {
            taskNode.setTaskType(TaskEnum.normal);

        }


        NodeList children = element.getChildNodes();
        Parser parse = NodeParser.getParserMap().get(NodeTypeEnum.actor.name());
        List<TaskActor> taskActorList = new ArrayList<TaskActor>();
        if (null != children) {
            for (int i = 0; i < children.getLength(); i++) {
                Node node = children.item(i);
                if (null != node && node.getNodeType() == Node.ELEMENT_NODE) {
                    TaskActor taskActor = (TaskActor) parse.parse((Element) node);
                    if (null != taskActor) {
                        taskActorList.add(taskActor);
                    }
                }
            }
        }
        taskNode.setActorList(taskActorList);
    }



    public void check(TaskNode taskNode){

    }
}
